home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Panorama / Panorama - Disk 16A (1987-04-08)(Pacific North-West Amigas Club)[b corrupt file][WB].zip / Panorama - Disk 16A (1987-04-08)(Pacific North-West Amigas Club)[b corrupt file][WB].adf / Ping.c < prev    next >
C/C++ Source or Header  |  1987-04-05  |  3KB  |  147 lines

  1. /*
  2.  * ping.c
  3.  *
  4.  * Yes, it's sloppy, but it works.
  5.  *
  6.  * By Chris Saulit,    28-Mar-87.
  7.  *
  8.  *    ...!ihnp4!alberta!uqv-mts!saul
  9.  *    CSAULIT@UALTAVM.BITNET
  10.  *    73007,3267 (CIS)
  11.  *
  12.  */
  13.  
  14. #include <intuition/intuition.h>
  15. #include <devices/input.h>
  16.  
  17. #define ESC    27
  18.  
  19. extern SHORT rnd();
  20.  
  21. struct Window *w=NULL, *OpenWindow();
  22. struct Screen *s;
  23.  
  24. #define FLAGS ACTIVATE|WINDOWDEPTH|WINDOWCLOSE|WINDOWDRAG|SIMPLE_REFRESH
  25.  
  26. struct NewWindow nw = {
  27.     200,12,200,10,            /* origin, dimensions */
  28.     -1,-1,                /* pens */
  29.     CLOSEWINDOW | VANILLAKEY,    /* IDCMP flags */
  30.     FLAGS,                /* Flags */
  31.     NULL, NULL,            /* FirstGadget, CheckMark */
  32.     "\253< Ping! >\273",        /* Title */
  33.     NULL, NULL,            /* Screen, BitMap */
  34.     0,0,0,0,            /* Min and Max dimensions */
  35.     WBENCHSCREEN            /* Type */
  36. };
  37.  
  38. ULONG    IntuitionBase, OpenLibrary();
  39. struct    MsgPort        *ioPort, *CreatePort();
  40. struct    IOStdReq    *ioReq,  *CreateStdIO();
  41. struct    InputEvent    fake;
  42.  
  43. _main()
  44. {
  45.    initstuff();
  46.    bounce();
  47.    cleanexit(0);
  48. }
  49.  
  50.  
  51. initstuff()
  52. {
  53.    if ( ! (IntuitionBase = OpenLibrary("intuition.library",0L)) )
  54.       cleanexit("No intuition?");
  55.  
  56.    if ( ! (ioPort = CreatePort("ping",NULL)) )
  57.       cleanexit("Can't create port.");
  58.  
  59.    if ( ! (ioReq = CreateStdIO(ioPort)) )
  60.       cleanexit("Can't create I/O req");
  61.  
  62.    if ( OpenDevice("input.device",0,ioReq,0) )
  63.       cleanexit("Can't open input.device");
  64.  
  65.    ioReq->io_Command    = IND_WRITEEVENT;
  66.    ioReq->io_Flags    = 0;
  67.    ioReq->io_Length    = sizeof(struct InputEvent);
  68.    ioReq->io_Data    = (APTR) &fake;
  69.  
  70.    fake.ie_NextEvent        = NULL;
  71.    fake.ie_Class        = IECLASS_RAWMOUSE;
  72.    fake.ie_TimeStamp.tv_secs    = 0;
  73.    fake.ie_TimeStamp.tv_micro    = 0;
  74.    fake.ie_Code            = IECODE_NOBUTTON;
  75.    fake.ie_Qualifier        = IEQUALIFIER_RELATIVEMOUSE;
  76.  
  77.    if ( ! (w = OpenWindow(&nw)) )
  78.       cleanexit("Can't open window.");
  79.  
  80.    s = w->WScreen;
  81.  
  82.    rnd( - s->MouseX );        /* plant a seed */
  83. }
  84.  
  85.  
  86. /* grav acceleration */
  87. #define    Y_ACC    1
  88. /* size of pointer */
  89. #define PTR_H    11
  90. #define PTR_W    11
  91.  
  92. bounce()
  93. {
  94.    ULONG class;
  95.    USHORT code;
  96.    struct IntuiMessage *iMsg;
  97.  
  98.    SHORT y_vel = 0,        /* start at rest */
  99.          x_vel = 0;
  100.  
  101.    for (;;) {
  102.       fake.ie_Y = y_vel;        /* relative motion */
  103.       fake.ie_X = x_vel;
  104.       DoIO(ioReq);
  105.       Delay(1);
  106.       if (iMsg = (struct IntuiMessage *) GetMsg(w->UserPort)) {
  107.          class = iMsg->Class;
  108.      code = iMsg->Code;
  109.          ReplyMsg(iMsg);
  110.          if (class == CLOSEWINDOW  ||  class==VANILLAKEY && code==ESC)
  111.             return;
  112.       }
  113.  
  114.       y_vel += Y_ACC;         /* increase downward velocity */
  115.  
  116.       if (s->MouseY >= s->Height-PTR_H) {    /* hit bottom */
  117.      x_vel += (rnd(9) - 4);
  118.          y_vel = -y_vel + (rnd(7) - 3) ;
  119.      y_vel += Y_ACC;
  120.       }
  121.       else if (s->MouseY <= 0) {    /* hit top */
  122.          y_vel = Y_ACC;
  123.       }
  124.       if (s->MouseX >= s->Width - PTR_W   ||   s->MouseX <= 0) {
  125.          x_vel = -x_vel;
  126.       }
  127.    }
  128. }
  129.  
  130.  
  131. cleanexit(msg)
  132.    char *msg;
  133. {
  134.    if (ioReq)
  135.       if (ioReq->io_Device)
  136.      CloseDevice(ioReq);
  137.       DeleteStdIO(ioReq);
  138.  
  139.    if (ioPort)
  140.       DeletePort(ioPort);
  141.  
  142.    if (w)        CloseWindow(w);
  143.    if (IntuitionBase)    CloseLibrary(IntuitionBase);
  144.  
  145.    _exit(0);
  146. }
  147.